﻿using Sys.Workflow.Bpmn.Models;
using Sys.Workflow.Engine.impl.runtime;
using Sys.Workflow.Engine.impl.util;
using Sys.Workflow.Engine.Impl.Interceptor;
using Sys.Workflow.Engine.Impl.Persistence.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sys.Workflow.Engine.dynamic
{
    /* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



    /**
     * @author Tijs Rademakers
     */
    public class DefaultDynamicStateManager : AbstractDynamicStateManager, IDynamicStateManager
    {
        public void MoveExecutionState(ChangeActivityStateBuilderImpl changeActivityStateBuilder, ICommandContext commandContext)
        {
            List<MoveExecutionEntityContainer> moveExecutionEntityContainerList = ResolveMoveExecutionEntityContainers(changeActivityStateBuilder,
                changeActivityStateBuilder.GetProcessInstanceVariables(), commandContext);
            List<IExecutionEntity> executions = null;
            if (moveExecutionEntityContainerList.Count() > 0)
            {
                executions = moveExecutionEntityContainerList.First().GetExecutions();
            }

            List<EnableActivityContainer> enableActivityContainerList = ResolveEnableActivityContainers(changeActivityStateBuilder);
            String processInstanceId = null;
            if (executions != null && executions.Count() > 0)
            {
                processInstanceId = executions.First().ProcessInstanceId;
            }
            else
            {
                processInstanceId = changeActivityStateBuilder.GetProcessInstanceId();
            }

            ProcessInstanceChangeState processInstanceChangeState = new ProcessInstanceChangeState()
                .SetProcessInstanceId(processInstanceId)
                .SetMoveExecutionEntityContainers(moveExecutionEntityContainerList)
                .SetEnableActivityContainers(enableActivityContainerList)
                .SetLocalVariables(changeActivityStateBuilder.GetLocalVariables())
                .SetProcessInstanceVariables(changeActivityStateBuilder.GetProcessInstanceVariables());

            DoMoveExecutionState(processInstanceChangeState, commandContext);
        }

        //protected Map<String, List<ExecutionEntity>> ResolveActiveEmbeddedSubProcesses(String processInstanceId, CommandContext commandContext)
        //{
        //    IExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
        //    List<IExecutionEntity> childExecutions = executionEntityManager.findChildExecutionsByProcessInstanceId(processInstanceId);

        //    Map<String, List<IExecutionEntity>> activeSubProcessesByActivityId = childExecutions.stream()
        //        .filter(ExecutionEntity::isActive)
        //        .filter(executionEntity->executionEntity.getCurrentFlowElement() instanceof SubProcess)
        //        .collect(Collectors.groupingBy(ExecutionEntity::getActivityId));
        //    return activeSubProcessesByActivityId;
        //}


        protected override bool IsDirectFlowElementExecutionMigration(FlowElement currentFlowElement, FlowElement newFlowElement)
        {
            return false;
        }

        protected override Dictionary<string, List<IExecutionEntity>> ResolveActiveEmbeddedSubProcesses(string processInstanceId, ICommandContext commandContext)
        {
            throw new NotImplementedException();
        }
    }
}

